package com.kukbab.c2dm;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;



public class ChatMessageView extends ListView implements OnItemClickListener{

	private Context mContext;
	private Activity mActivity;
	public ArrayList<String[]> list = new ArrayList<String[]>();
	
	private ChatMessageAdapter adapter;

	private LinearLayout layout;
	public TextView chatName;
	public TextView chatMessage;
	private LayoutInflater inflate;
	
	public ChatMessageView(Context context) {
		super(context);
		mContext = context;
		mActivity = (Activity)context;
		inflate = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		adapter = new ChatMessageAdapter();
		setAdapter(adapter);
		setOnItemClickListener(this);
		
	}
	public void refreshData(){		
		adapter.notifyDataSetChanged();
	}
	public class ChatMessageAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			return list == null ? 0 : list.size();
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			if(convertView ==null){
				layout  = (LinearLayout)inflate.inflate(R.layout.chat_msg_row, null);
			}else{
				layout =(LinearLayout)convertView;	
			}
			chatName = (TextView)layout.findViewById(R.id.chat_name);
			chatMessage = (TextView)layout.findViewById(R.id.chat_message);
			
			String[] data = list.get(position);
			chatName.setText(data[0]);
			chatMessage.setText(data[1]);
			if(data[2].equals("left")){
				chatName.setGravity(Gravity.LEFT);
				chatMessage.setGravity(Gravity.LEFT);
			}else{
				chatName.setGravity(Gravity.RIGHT);
				chatMessage.setGravity(Gravity.RIGHT);
			}
			return layout;
		}
		
	}
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
		
	}
}
